$(function () {
    $(".head").load("head.html")
    $(".top").load("gw.html")

    $.post("news/gouwuche",{},function (data) {

        console.log(data)
        var prices=0;
        $.each(data.list,function (i,news) {
           if (i%2==0){
               $(".car_tab").find('tbody').append(
                   "            <tr class=\"car_tr\">\n"+
                   "                <td>\n" +
                   "                    <div class=\"c_s_img\"><img src='../../files/"+news.filename+"' width=\"73\" height=\"73\" /></div>\n" +
                   "                   "+news.name+"\n库存："+news.stock+"" +
                   "                </td>\n" +
                   "                <td align=\"center\">\n" +
                   "                    <div class=\"c_num\">\n" +
                   "                        <input type=\"button\"  data-num='"+news.num+"' data-id='"+news.id+"' value=\"\" onclick=\"jianUpdate1(jq(this));\" class=\"car_btn_1\" />\n" +
                   "                        <input type=\"text\"  value='"+news.num+"' name=\"\" class=\"car_ipt\" />\n" +
                   "                        <input type=\"button\" data-stock='"+news.stock+"'  data-num='"+news.num+"' data-id='"+news.id+"' value=\"\" onclick=\"addUpdate1(jq(this));\" class=\"car_btn_2\" />\n" +
                   "                    </div>\n" +
                   "                </td>\n" +
                   "                <td align=\"center\" style=\"color:#ff4e00;\">￥"+news.price*news.num+"</td>\n" +
                   "                <td align=\"center\"><a href=\"javascript:;\" data-id='"+news.id+"' class='shanchu'>删除</a>&nbsp; &nbsp;<a href=\"javascript:;\" data-id='"+news.id+"' class='shouc'>加入收藏</a></td>\n" +
                   "            </tr>\n"
               )
           }
           else {
               $(".car_tab").find('tbody').append(
                   "            <tr>\n"+
                   "                <td>\n" +
                   "                    <div class=\"c_s_img\"><img src='../../files/"+news.filename+"' width=\"73\" height=\"73\" /></div>\n" +
                   "                   "+news.name+"\n库存："+news.stock+"" +
                   "                </td>\n" +
                   "                <td align=\"center\">\n" +
                   "                    <div class=\"c_num\">\n" +
                   "                        <input type=\"button\" data-num='"+news.num+"' data-id='"+news.id+"' value=\"\" onclick=\"jianUpdate1(jq(this));\" class=\"car_btn_1\" />\n" +
                   "                        <input type=\"text\" value='"+news.num+"' name=\"\" class=\"car_ipt\" />\n" +
                   "                        <input type=\"button\" data-stock='"+news.stock+"'  data-num='"+news.num+"' data-id='"+news.id+"' value=\"\" onclick=\"addUpdate1(jq(this));\" class=\"car_btn_2\" />\n" +
                   "                    </div>\n" +
                   "                </td>\n" +
                   "                <td align=\"center\" style=\"color:#ff4e00;\">￥"+news.price*news.num+"</td>\n" +
                   "                <td align=\"center\"><a href=\"javascript:;\" data-id='"+news.id+"' class='shanchu'>删除</a>&nbsp; &nbsp;<a href=\"javascript:;\" data-id='"+news.id+"' class='shouc'>加入收藏</a></td>\n" +
                   "            </tr>\n"
               )
           }
           prices+=news.price*news.num;
        })
        $(".car_tab").find("tbody").append(
            "          <tr height=\"70\">\n" +
            "          \t<td colspan=\"6\" style=\"font-family:'Microsoft YaHei'; border-bottom:0;\">\n" +
            "                <span class=\"fr\">商品总价：<b style=\"font-size:22px; color:#ff4e00;\">￥"+prices+"</b></span>\n" +
            "            </td>\n" +
            "          </tr>\n" +
            "          <tr valign=\"top\" height=\"150\">\n" +
            "          \t<td colspan=\"6\" align=\"right\">\n" +
            "            \t<a href=\"http://localhost:7777\"><img src=\"images/buy1.gif\" /></a>&nbsp; &nbsp; <a class='jisuGW' href=\"javascript:;\"><img src=\"images/buy2.gif\" /></a>\n" +
            "            </td>\n" +
            "          </tr>"
        )
        $(".shanchu").on('click',function () {
            if (confirm("是否确认删除？")){
                $.post("news/shanchu",{"id":$(this).data('id')},function (data) {
                    alert(data.msg)
                    location.href="http://localhost:7777/BuyCar.html"
                })
            }
        })
        $(".shouc").on('click',function () {
            $.post("news/scSP",{"id":$(this).data('id')},function (data) {
                    alert(data.msg)
            })

        })
        $(".car_btn_1").on('click',function () {
            if ($(this).data('num')-1<1){
                $(this).nextSibling
            }else {
                $.post("news/xiugaiNum",{"id":$(this).data('id'),"num":$(this).data('num')-1},function (data) {
                    alert(data.msg)
                    location.href="http://localhost:7777/BuyCar.html"
                })
            }
        })
        $(".car_btn_2").on('click',function () {
            if ($(this).data('num')+1>$(this).data('stock')){
                alert("库存不足")
                location.href="http://localhost:7777/BuyCar.html"
            }else {
                $.post("news/xiugaiNum",{"id":$(this).data('id'),"num":$(this).data('num')+1},function (data) {
                    alert(data.msg)
                    location.href="http://localhost:7777/BuyCar.html"
                })
            }

        })
        $(".jisuGW").on('click',function () {
               if (prices==0){
                   alert("没有商品被加入购物车，请返回加入购物车后再做此操作！")
                   location.href="http://localhost:7777";
               }else {
                   location.href="http://localhost:7777/BuyCar_Two.html";
               }
        })

    })
})